package upcweb.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import upcweb.exception.DAOExcepcion;
import upcweb.modelo.ClsCotizacion;
import upcweb.modelo.ClsServicio;
import upcweb.modelo.ClsSolicitud;
import upcweb.modelo.ClsTipoServicio;
import upcweb.negocio.BsGestionCotizacion;
import upcweb.negocio.BsGestionServicio;
import upcweb.util.ClsReturn;

@Controller
public class CotizacionController {

	BsGestionCotizacion bsCot = new BsGestionCotizacion();
	
	@RequestMapping(value = "/listarSolicitudes" , method = RequestMethod.POST)
	protected ModelAndView listarSolicitudes(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView view = new ModelAndView("cotizaciones/mvListarSolicitudes");
		String fecha1 = "";
		String fecha2 = "";
		String respon = "";
		String script= "";
		int club = 0;
		ArrayList<ClsCotizacion> arrSol = new ArrayList<ClsCotizacion>();
		if (request.getParameter("search")==null){
		arrSol = bsCot.getSolicitudes();
		}
		else {
			ClsCotizacion cot=new  ClsCotizacion();
			fecha1 = request.getParameter("fecha1");
			fecha2 = request.getParameter("fecha2");			
			respon = request.getParameter("respon");
			club = Integer.parseInt(request.getParameter("club"));
			
			try {
				//12/12/2012
				// Transformar FMT Fecha
				//fecha1 = ;
				//fecha2 = fecha2.substring(6, 10) + "-" + fecha2.substring(3,5) + "-" + fecha2.substring(0,2);
				cot.setFec_Inicio(fecha1.substring(6, 10) + "-" + fecha1.substring(3,5) + "-" + fecha1.substring(0,2));
				cot.setFec_Final(fecha2.substring(6, 10) + "-" + fecha2.substring(3,5) + "-" + fecha2.substring(0,2));
			} catch (Exception e) {
				// TODO: handle exception
				script= "ShowError('El Formato de Fechas Ingresado es incorrecto.";
			}
			

			cot.setCo_club(club);
			cot.setNo_Responsable(respon);
			arrSol = bsCot.filterCotizacion(cot);
		}
		view.addObject("clubs",bsCot.getClub());
		view.addObject("datos", arrSol);
		view.addObject("f1",fecha1);
		view.addObject("f2",fecha2);
		view.addObject("res",respon);
		view.addObject("script",script);
		//view.addObject("cl",club);
		return view;
	}
	
	@RequestMapping(value = "/verCotizacion", method = RequestMethod.POST)
	protected ModelAndView verCotizacion(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{
		ModelAndView view = new ModelAndView("cotizaciones/mvVerCotizacion");
		ClsCotizacion clsCot = bsCot.getCotizacion(new ClsSolicitud(Integer.parseInt(request.getParameter("solicitud"))));
		String search = request.getParameter("search");
		if (search == null ){
		view.addObject("servicios",new BsGestionServicio().listar());
		view.addObject("act1","active");
		view.addObject("act2","");
		}
		else{
		if (Integer.parseInt(search) > 0){	
		view.addObject("servicios",new BsGestionServicio().listarPorTipo(new ClsTipoServicio(Integer.parseInt(search), "", "", null)));		
		}
		else {
			view.addObject("servicios",new BsGestionServicio().listar());
		}
		view.addObject("act1","");
		view.addObject("act2","active");
		}
		view.addObject("cotizacion", clsCot);
		return view;
	}
	
	@RequestMapping(value = "/opServCotizacion", method = RequestMethod.POST)
	protected ModelAndView operarCotizacion(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{
		ModelAndView view = new ModelAndView("cotizaciones/opServCotizacion");
		//ClsCotizacion clsCot = bsCot.getCotizacion(new ClsSolicitud(Integer.parseInt(request.getParameter("solicitud"))));
		String oper = request.getParameter("operacion");
		ClsServicio ser = new ClsServicio();
		ClsReturn ret = new ClsReturn();
		if (oper.equalsIgnoreCase("cotizar")){
			ClsCotizacion cot = new ClsCotizacion();
			cot.setNu_solicitud(Integer.parseInt(request.getParameter("cotiza")));
			ret = new BsGestionCotizacion().setCotizado(cot);
		}
		else if (oper.equalsIgnoreCase("add") ){
		ser.setNo_Solicitud(Integer.parseInt(request.getParameter("cotizacion")));
		ser.setIdService(Integer.parseInt(request.getParameter("servicio")));
		ser.setObservaciones(request.getParameter("texto"));
		ser.setMinParticipants(Integer.parseInt(request.getParameter("cantidad")));
		ser.setCostPerPerson(Double.parseDouble(request.getParameter("costo")));
		ret = new BsGestionCotizacion().addItemsCotizacion(ser);
		}
		else if (oper.equalsIgnoreCase("edit") ){
			ser.setNo_Solicitud(Integer.parseInt(request.getParameter("cotizacion")));
			ser.setIdService(Integer.parseInt(request.getParameter("servicio")));
			ser.setObservaciones(request.getParameter("texto"));
			ser.setMinParticipants(Integer.parseInt(request.getParameter("cantidad")));
			ser.setCostPerPerson(Double.parseDouble(request.getParameter("costo")));
			ret = new BsGestionCotizacion().editItemsCotizacion(ser);
			
		}
		else if (oper.equalsIgnoreCase("del")){
			ser.setNo_Solicitud(Integer.parseInt(request.getParameter("cotizacion")));
			ser.setIdService(Integer.parseInt(request.getParameter("servicio")));	
			ret = new BsGestionCotizacion().delItemsCotizacion(ser);
		}
		//view.addObject("servicios",new BsGestionServicio().listar());
		view.addObject("operacion",oper);
		view.addObject("cotizacion",ser.getNo_Solicitud());
		view.addObject("return", ret);
		return view;
	}
	
	
}
